package com.voicechat.firstmet.utils;

import android.media.MediaPlayer;

import java.io.File;
import java.io.IOException;

/**
 * @ProjectName: FirstMet
 * @Package: com.voicechat.firstmet.utils
 * @ClassName: AudioPlayerUtil
 * @Description: 播放语音
 * @Author: ljp
 * @CreateDate: 2018/9/19 14:19
 * @Version: 1.0
 */
public class AudioPlayerUtil {
    private static final String TAG = "AudioRecordTest";
    private MediaPlayer mPlayer;
    private String mFileName;

    public AudioPlayerUtil() {
    }

    /*public void start(String mFileName, MediaPlayer.OnCompletionListener listener) {
        if(this.mPlayer == null) {
            this.mPlayer = new MediaPlayer();
        } else {
            this.mPlayer.reset();
        }
        try {
            this.mPlayer.setDataSource(mFileName);
            this.mPlayer.prepare();
            this.mPlayer.start();

            if(listener != null) {
                this.mPlayer.setOnCompletionListener(listener);
            }
        } catch (Exception var4) {
        }
    }*/
    public void start(String mFileName) {
        if(this.mPlayer == null) {
            this.mPlayer = new MediaPlayer();
        } else {
            this.mPlayer.reset();
        }
        try {
            this.mPlayer.setDataSource(mFileName);
            this.mPlayer.prepare();
            this.mPlayer.start();
            /*if(listener != null) {
                this.mPlayer.setOnCompletionListener(listener);
            }*/
        } catch (Exception var4) {
        }
    }
    /**
    * @method
    * @description 播放
    * @date: 2018/9/29 11:46
    * @author: ljp
    * @return
    */
    public void startPlay(MediaPlayer.OnCompletionListener listener){
        if(listener != null) {
            this.mPlayer.setOnCompletionListener(listener);
        }
    }
    /**
    * @method
    * @description 判断是否在播放
    * @date: 2018/9/29 15:02
    * @author: ljp
    * @return
    */
    public boolean setPlayer(){
        if(this.mPlayer != null){
            return this.mPlayer.isPlaying();
        }
        return false;
    }
    /**
    * @method
    * @description 获取时长
    * @date: 2018/9/29 11:40
    * @author: ljp
    * @return
    */
    public int getTime(File path){
        if(mPlayer == null){
            mPlayer = new MediaPlayer();
            try {
                mPlayer.setDataSource(path.getPath());
                mPlayer.prepare();
            } catch (IOException e) {
                e.printStackTrace();
            }
            return mPlayer.getDuration();
        }else {
            return this.mPlayer.getDuration();
        }
    }
    /**
     * @method
     * @description 获取时长
     * @date: 2018/9/29 11:40
     * @author: ljp
     * @return
     */
    public int getTime(String path){
        if(mPlayer == null){
            mPlayer = new MediaPlayer();
            try {
                mPlayer.setDataSource(path);
                mPlayer.prepare();
            } catch (IOException e) {
                e.printStackTrace();
            }
            return mPlayer.getDuration();
        }else {
            return this.mPlayer.getDuration();
        }
    }
    /*public int setFirst(String path){

    }*/

    public void stop() {
        if(this.mPlayer != null) {
            this.mPlayer.stop();
            this.mPlayer.release();
            this.mPlayer = null;
        }
    }
}
